const axios = require('axios')

async function ipToCity(ip) {
  try {
    // 使用高德api进行IP转地址
    const apiKey = 'f2b1e061b1a259811cf810af001fed1c'
    const response = await axios.get(
      `https://restapi.amap.com/v3/ip?ip=${ip}&key=${apiKey}`
    )
    console.log('response', response)

    if (response.data.status === '1') {
      const { city } = response.data
      return `${city}`
    }

    return '未知地区'
  } catch (error) {
    console.error('IP地址解析失败:', error)
    return '未知地区'
  }
}

module.exports = ipToCity
